NAME = "Rock5s Stanalone Multihacks"
VERSION = "0.3"

include("addresses.lua");
include("settings.lua");
include("functions.lua");
include("objectlist.lua");

local HackState = {
	Fly = OFF,
	Wall = OFF,
	Speed = {
		Running = {SetValue = default_high_running_speed, Enabled = false},
		Mounted = {SetValue = default_high_mounted_speed, Enabled = false},
	},
	Show_Stealthed = OFF,
}

function exitCallback()
	printf("\n")
	setupConsole("reset")
end
atExit(exitCallback);
atError(exitCallback);

function printoutupdate()
	if not isInGame() then return end

	local function namekey(name,value)
		return string.format("|yellow|%s: %s   ",name,value)
	end

	local speed
	if HackState.Speed.Mounted.Enabled then
		speed = string.format("|lightgreen|%-3d", HackState.Speed.Mounted.SetValue)
	elseif HackState.Speed.Running.Enabled then
		speed = string.format("|lightgreen|%-3d", HackState.Speed.Running.SetValue)
	else
		speed = OFF
	end

	cprintf_ex(namekey("MobStealth",HackState.Show_Stealthed)..
			   namekey("Fly",HackState.Fly)..
			   namekey("Wall",HackState.Wall)..
			   namekey("Speed",speed)..
			   "|lightgreen|("..getKeyName(hotkeys.HELP.key)..") Help.\r")

end

local helpShowing = false
function showhelp()
	if helpShowing then
		printf("\n")
		setupConsole(80,1)
	else
		setupConsole(80,10)
		cprintf(cli.lightgreen,"\n\n   Rock5s Multihack v%s\n\n",VERSION)
		cprintf_ex("|yellow|Toggle mob stealth\t|white|%s\n",getKeyName(hotkeys.SEE_STEALTH_MOBS.key))
		cprintf_ex("|yellow|Toggle fly\t|white|%s\n",getKeyName(hotkeys.FLY_TOGGLE.key))
		cprintf_ex("|yellow|Toggle wall\t|white|%s\n",getKeyName(hotkeys.WALL_TOGGLE.key))
		cprintf_ex("|yellow|Speed down\t|white|%s\n",getKeyName(hotkeys.SPEED_DOWN.key))
		cprintf_ex("|yellow|Toggle speed\t|white|%s\n",getKeyName(hotkeys.SPEED_TOGGLE.key))
		cprintf_ex("|yellow|Speed up\t|white|%s\n",getKeyName(hotkeys.SPEED_UP.key))
		print()
	end
	helpShowing = not helpShowing
end

local oldW, oldH, oldX, oldY
if getConsoleAttributes then
	oldW, oldH, oldX, oldY = getConsoleAttributes()
end

function setupConsole(w,h)
	if setConsoleAttributes then
		if w == "reset" then
			setConsoleAttributes(oldW,oldH)
		else
			local tmpW,tmpH, tmpX,tmpY = getConsoleAttributes()
			if w > tmpX then
				setConsoleAttributes(tmpW,tmpH,w,tmpY)
			end
			setConsoleAttributes(w,h)
		end
	end
end

function main()
	attach(getWin());

	if( not checkExecutableCompatible() ) then
		printf("Addresses seem off, trying to update.\n")
		include("update.lua")
		if( not checkExecutableCompatible() ) then
			error("The addresses didn't update, you will have to wait until there is an SVN update that updates the addresses.\n")
		else
			cprintf(cli.yellow,"update.lua seems to have worked, addresses.lua has been updated.\n")
		end
	end

	setWindowName(getHwnd(),NAME.." - " .. VERSION)
	if setWindowPos then
		local gameX,gameY = windowRect(getWin())
		setWindowPos(getHwnd(), gameX+position_X, gameY+position_Y,nil, nil, true)
	end
	setupConsole(80,1)

	printoutupdate()

	local hf_key_pressed, hf_key;
	while(true) do

		hf_key_pressed = false;

		if keyComboPressed(hotkeys.HELP) then
			hf_key_pressed = true;
			hf_key = "HELP";
		end;
		if keyComboPressed(hotkeys.FLY_TOGGLE) then
			hf_key_pressed = true;
			hf_key = "FLY_TOGGLE";
		end;
		if keyComboPressed(hotkeys.WALL_TOGGLE) then
			hf_key_pressed = true;
			hf_key = "WALL_TOGGLE";
		end;
		if keyComboPressed(hotkeys.SPEED_TOGGLE) then
			hf_key_pressed = true;
			hf_key = "SPEED_TOGGLE";
		end;
		if keyComboPressed(hotkeys.SPEED_UP) then
			hf_key_pressed = true;
			hf_key = "SPEED_UP";
		end;
		if keyComboPressed(hotkeys.SPEED_DOWN) then
			hf_key_pressed = true;
			hf_key = "SPEED_DOWN";
		end;
		if keyComboPressed(hotkeys.SEE_STEALTH_MOBS) then
			hf_key_pressed = true;
			hf_key = "STEALTH";
		end;


		if( hf_key_pressed == false and 	-- key released, do the work
			hf_key ) then					-- and key not empty

			if( hf_key == "HELP" ) then
				showhelp();
			end;
			if( hf_key == "FLY_TOGGLE" ) then
				HackState.Fly = flytoggle();
			end;
			if( hf_key == "WALL_TOGGLE" ) then
				HackState.Wall = walltoggle();
			end;
			if( hf_key == "SPEED_TOGGLE" ) then
				if isMounted() then
					speedtoggle(HackState.Speed.Mounted.Enabled, HackState.Speed.Mounted.SetValue)
					HackState.Speed.Mounted.Enabled = not HackState.Speed.Mounted.Enabled
					HackState.Speed.Running.Enabled = false
				else
					speedtoggle(HackState.Speed.Running.Enabled, HackState.Speed.Running.SetValue)
					HackState.Speed.Running.Enabled = not HackState.Speed.Running.Enabled
					HackState.Speed.Mounted.Enabled = false
				end
			end;
			if( hf_key == "SPEED_UP" ) then
				if isMounted() then
					HackState.Speed.Mounted.Enabled = true
					HackState.Speed.Running.Enabled = false
					HackState.Speed.Mounted.SetValue = speedup() or HackState.Speed.Mounted.SetValue
				else
					HackState.Speed.Running.Enabled = true
					HackState.Speed.Mounted.Enabled = false
					HackState.Speed.Running.SetValue = speedup() or HackState.Speed.Running.SetValue
				end
			end;
			if( hf_key == "SPEED_DOWN" ) then
				if isMounted() then
					HackState.Speed.Mounted.Enabled = true
					HackState.Speed.Running.Enabled = false
					HackState.Speed.Mounted.SetValue = speeddown() or HackState.Speed.Mounted.SetValue
				else
					HackState.Speed.Running.Enabled = true
					HackState.Speed.Mounted.Enabled = false
					HackState.Speed.Running.SetValue = speeddown() or HackState.Speed.Running.SetValue
				end
			end;
			if( hf_key == "STEALTH" ) then
				HackState.Show_Stealthed = (HackState.Show_Stealthed == ON) and OFF or ON
			end
			hf_key = nil;	-- clear last pressed key
			printoutupdate()
		end
		yrest(100)
		if keep_speed_after_zoning then
			if HackState.Speed.Mounted.Enabled then
				setSpeed(HackState.Speed.Mounted.SetValue)
			elseif HackState.Speed.Running.Enabled then
				setSpeed(HackState.Speed.Running.SetValue)
			end
		end

		if HackState.Show_Stealthed == ON then
			showStealthedMobs()
		end

	end

end

startMacro(main,true);
